# -*- coding: utf-8 -*-
import scrapy
from scrapy import Request
from boss_zhipin.items import BossZhipinItem


class BossSpider(scrapy.Spider):
    name = 'boss'
    allowed_domains = ['www.zhipin.com']
    start_urls = 'https://www.zhipin.com/i100021-c100010000/?page=1'
    bash_url = 'https://www.zhipin.com'

    def start_requests(self):
        yield Request(url=self.start_urls, callback=self.parse)

    def parse(self, response):
        lis = response.xpath('//div[@class="job-list"]/ul/li')
        next_page = response.xpath('//div[@class="page"]/a[last()]/@href').extract()[0]
        if next_page != 'javascript:;':
            yield Request(url=self.bash_url+next_page, callback=self.parse)
        for li in lis:
            info_url = li.xpath('.//div[@class="job-primary"]//h3[@class="name"]/a/@href').extract()[0]
            yield Request(url=self.bash_url+info_url, callback=self.info_parse)

    def info_parse(self, response):
        item = BossZhipinItem()
        # 公司信息
        info_company = response.xpath('//div[@class="job-primary"]/div[@class="info-company"]')
        item['company_name'] = info_company.xpath('.//h3[@class="name"]/a/text()').extract()[0]
        item['company_list'] = info_company.xpath('.//p[1]/text()').extract()[0]
        item['company_scale'] = info_company.xpath('.//p[1]/text()').extract()[1]
        item['company_brandindustry'] = info_company.xpath('.//p[1]/a/text()').extract()[0]
        item['company_website'] = info_company.xpath('.//p[last()]/text()').extract()[0]
        # 招聘信息
        info_primary = response.xpath('//div[@class="job-primary"]/div[@class="info-primary"]')
        item['job_name'] = info_primary.xpath('.//h1/text()').extract()[0]
        item['job_wages'] = info_primary.xpath('.//h1/span/text()').extract()[0]
        item['job_place'] = info_primary.xpath('.//p/text()').extract()[0]
        item['job_education'] = info_primary.xpath('.//p/text()').extract()[2]
        item['job_sec'] = response.xpath('//div[@class="job-box"]//div[@class="text"]').xpath('string(.)').extract()[0]
        yield item


